﻿//  Copyright (c) 2010 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Mvc;

namespace DNA.Mvc.jQuery
{
    /// <summary>
    /// Defines the ListBox Component object
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    public class ListBox<TModel> : RemoteDataBindableContainer<TModel, NavigableView>
        where TModel : class
    {
        /// <summary>
        /// Gets / Sets the list items.
        /// </summary>
        public IList<NavigableView> Items
        {
            get
            {
                return InnerItems;
            }
            set
            {
                InnerItems = value;
            }
        }

        /// <summary>
        /// Gets / Sets the current value of the listbox ,when this property set ListBox will auto select the item which value equal Value property.
        /// </summary>
        public string Value { get; set; }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            base.RenderBeginTag(writer);
            var input = new TagBuilder("input");
            input.AddCssClass("d-listbox-values");
            input.MergeAttribute("type", "hidden");
            input.MergeAttribute("name", this.Name);
            if (!string.IsNullOrEmpty(Value))
                input.MergeAttribute("value", Value);
            writer.Write(input.ToString(TagRenderMode.SelfClosing));
        }
        
        public IDictionary<string, object> ItemHtmlAttributes { get; set; }

        public override void RenderContent(HtmlTextWriter writer)
        {
            writer.WriteFullBeginTag("ul");
            if (string.IsNullOrEmpty(RemoteDataSource) && InnerItems.Count > 0)
            {
                foreach (var item in InnerItems)
                {

                    if (!ItemTemplate.IsEmpty)
                    {
                        writer.WriteBeginTag("li");
                        if (ItemHtmlAttributes != null)
                        {
                            foreach (var key in ItemHtmlAttributes.Keys)
                            {
                                if (ItemHtmlAttributes[key].GetType().Equals(typeof(bool)))
                                    writer.WriteAttribute(key, ItemHtmlAttributes[key].ToString().ToLower());
                                else
                                    writer.WriteAttribute(key, ItemHtmlAttributes[key].ToString());
                            }
                        }
                        writer.Write(HtmlTextWriter.TagRightChar);
                        writer.WriteBeginTag("input");
                        writer.WriteAttribute("type", "hidden");
                        writer.WriteAttribute("value", item.Value.ToString());
                        writer.Write(HtmlTextWriter.SelfClosingTagEnd);

                        ItemTemplate.WriteTo((TModel)item.Model, writer);
                        writer.WriteEndTag("li");
                    }
                    else
                    {
                        if (ItemHtmlAttributes != null)
                            item.MergeAttributes(ItemHtmlAttributes);
                        item.Render(writer);
                    }
                }
            }
            writer.WriteEndTag("ul");
        }
    }
}
